package example.step4;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class MethodSignature {
    public static String getMethodSignature(Class<?>[] paramTypes,
                                            Class<?> retType) {
        StringBuilder sbuf = new StringBuilder();
        sbuf.append('(');
        for (int i = 0; i < paramTypes.length; i++) {
            sbuf.append(FieldSignature.getClassSignature(paramTypes[i]));
        }
        sbuf.append(')');
        sbuf.append(FieldSignature.getClassSignature(retType));
        return sbuf.toString();
    }
    public static void main(String args[]){
        // 构造函数签名
        Constructor<?>[] cons = MethodTest.class.getDeclaredConstructors();
        for( Constructor<?> con: cons){
            System.out.println("==>" + con.getName());
            System.out.println(getMethodSignature(con.getParameterTypes(),Void.TYPE));
        }
        // 方法签名
        Method[] methods = MethodTest.class.getDeclaredMethods();
        for( Method method: methods){
            System.out.println("==>" + method.getName());
            System.out.println(getMethodSignature(method.getParameterTypes(),method.getReturnType()));
        }
    }
}

class MethodTest{
    private int age;
    private String name;
    public MethodTest(int age, String name){
        this.age = age;
        this.name = name;
    }
    public MethodTest(int age){
        this(age,"Unknown");
    }
    public String buildTemplate(String prefix){
        return prefix + " " + this.name + ", you are " + this.age + " now.";
    }
    public void buildHello(){
        String template = buildTemplate("Hello");
        System.out.println(template);
    }
}